/**
 * 
 */
package com.cplsystems.incidencias.vm.cuentas.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Window;

import com.cplsystems.incidencias.app.utils.IncidenciasUtils;
import com.cplsystems.incidencias.business.Organizacion;
import com.cplsystems.incidencias.services.OrganizacionService;

/**
 * @author César Palalía López (csr.plz@aisa-automation.com)
 * 
 */
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class BuscarCuentaVM {

	@Wire("#companiasModalDialog")
	private Window companiasModalDialog;
	private List<Organizacion> companias;
	private Organizacion organizacionSeleccionada;

	@WireVariable
	private OrganizacionService organizacionService;

	private String compania;
	private String rfc;

	@Init
	public void init(@ContextParam(ContextType.VIEW) Component view) {
		Selectors.wireComponents(view, this, false);
		companias = new ArrayList<Organizacion>();
	}

	@Command
	@NotifyChange({ "companias" })
	public void buscarCompania() {
		if (compania != null && !compania.isEmpty()) {
			if (rfc != null && !rfc.isEmpty()) {
				companias = organizacionService.getCompaniasByNombreRFC(
						compania, rfc);
			} else {
				companias = organizacionService.getCompaniasByNombre(compania);
			}
		} else {
			if (rfc != null && !rfc.isEmpty()) {
				companias = organizacionService.getCompaniasByRFC(rfc);
			} else {
				IncidenciasUtils.showMessage(
						"Por favor especifique un parámetro de búsqueda",
						Clients.NOTIFICATION_TYPE_WARNING, 4500);
			}
		}
	}

	@Command
	public void transferirCompania() {
		if (organizacionSeleccionada == null) {
			IncidenciasUtils.showMessage(
					"Por favor seleccione un elemento antes de continuar",
					Clients.NOTIFICATION_TYPE_WARNING, 4500);
			return;
		}
		companiasModalDialog.detach();
		Map<String, Object> args = new HashMap<String, Object>();
		args.put("organizacionSeleccionada", organizacionSeleccionada);
		BindUtils.postGlobalCommand(null, null, "loadDatosCompania", args);	
	}

	@Command
	public void closeDialog() {
		if (companiasModalDialog != null) {
			companiasModalDialog.detach();
		}
	}

	public String getCompania() {
		return compania;
	}

	public void setCompania(String compania) {
		this.compania = compania;
	}

	public String getRfc() {
		return rfc;
	}

	public void setRfc(String rfc) {
		this.rfc = rfc;
	}

	public List<Organizacion> getCompanias() {
		return companias;
	}

	public void setCompanias(List<Organizacion> companias) {
		this.companias = companias;
	}

	public Organizacion getOrganizacionSeleccionada() {
		return organizacionSeleccionada;
	}

	public void setOrganizacionSeleccionada(
			Organizacion organizacionSeleccionada) {
		this.organizacionSeleccionada = organizacionSeleccionada;
	}

}
